<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 部门管理模型
 * @author PHP@妖孽<dino_ma@163.com>
 */

class AuthDepartmentModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
    	array('department_name','','帐号名称已经存在！', self::MUST_VALIDATE,'unique',self::MODEL_BOTH), 
        array('department_name', 'require', '部门名称必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('department_name', '1,20', '部门名称长度不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('description', '0,200', '部门简介长度不能超过200个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('sort', 'require', '排序名称必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('sort', 'number', '排序字段必须为数字', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('father_id', 'require', '必备关键字段必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('father_id', 'number', '必备关键字段必须为数字', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('status',array(-1,0,1),'status值的范围不正确！',2,'in'), 
	
    );
  
    /* 自动完成规则 */
    protected $_auto = array(
        array('status', 1, self::MODEL_INSERT, 'string'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
    	array('create_time', 'time', self::MODEL_INSERT, 'function'),
    );


    
    /**
     * 新增或更新一个活动
     * @return boolean fasle 失败 ， int  成功 返回完整的数据
     */
    public function update(){
        $data = $this->create($_POST);
        if(empty($data)){
            return false;
        }
        if(empty($data['id'])){
            $id = $this->add(); 
            if(!$id){
                $this->error = '新增部门出错！';
                return false;
            }
            action_log('department_add', 'department', $id , UID);
        } else { //更新数据
            $status = $this->where(array('id'=>$data['id']))->save(); 
            if(false === $status){
                $this->error = '更新部门出错！';
                return false;
            }
            action_log('department_edit', 'department', $data['id'] , UID);
        }
        return $data;
    }

}
